<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input"  id="userId" type="hidden"
                   name="userId" th:value="${informations.userId}"
                   placeholder="id" style="background:#dedede">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">当前角色</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input"  disabled="disabled"
                    th:value="${informations.roleName}"
                   placeholder="当前角色" style="background:#dedede" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分配角色</label>
        <div class="layui-input-block">
            <select id="allRole" name="userRoleId">
                <option value="">全部</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var datas = data.field;
            var index = layer.alert("确认提交？", {
                title: '提示'
            }, function () {
                $.ajax({
                    url: '/user/distributeRole',
                    type: "POST",
                    data: datas,
                    success: function (data) {
                        if (data.code == 200) {
                            layer.alert(data.message,{
                                title: "提示",
                            },function (){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = layer.getFrameIndex(window.name);
                                layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        }else{
                            layer.alert(data.message,{
                                title: "提示",
                            },function (){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = layer.getFrameIndex(window.name);
                                layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        }
                    }
                });

            });
            return false;
        });
        $(window).load(function () {
            $.ajax({
                //动态获取下拉选项的接口，返回数据主要是id+name 需要同步方便回写
                url: '/role/queryRole',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (data) {
                    if (data.code == 0) {
                        let str="<option value=''>全部</option>";
                        for(let role of data.data){
                            //组装数据
                            str+=`<option value='${role.roleId}'>${role.roleName}</option>`;
                        }
                        //jquery赋值方式
                        $("#allRole").html(str);
                        //重新渲染生效
                        form.render();
                    } else {
                        layer.alert(data.message,{
                            title: "提示",
                        },function (){
                            // 关闭弹出层
                            layer.close(index);
                        })
                    }
                }
            });

            var roleId = [[${informations.roleId}]];
            console.log(roleId)
            $("#allRole").each(function() {
                // this代表的是<option></option>，对option再进行遍历
                $(this).children("option").each(function() {
                    // 判断需要对那个选项进行回显
                    if (this.value == roleId) {
                        // 进行回显
                        $(this).attr("selected","selected");
                    }
                });
                form.render('select');
            })
        })
    });
</script>
</body>
</html>